define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'purchase/goods/index' + location.search,
                    // add_url: 'purchase/goods/add',
                    edit_url: 'purchase/goods/edit',
                    del_url: 'purchase/goods/del',
                    // multi_url: 'purchase/goods/multi',
                    // table: 'purchase_goods',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true,formatter:function(value,row){
                                if(row['status'] != '待处理'){
                                    return {
                                        disabled: true,
                                    }
                                }
                                // return '<input data-index="0" name="btSelectItem" type="checkbox">';
                            }},
                        {field: 'id', title: __('Id'),class:'gid'},
                        {field: 'title', title: __('Goods.title'),class: 'title'},
                        {field: 'number', title: __('Goods.number'),operate: false},
                        {field: 'specs', title: __('Goods.specs'),class: 'specs'},
                        {field: 'stock', title: __('Goods.stock'), operate: false},
                        {field: 'snick', title: __('Snick')},
                        {field: 'goods_num', title: __('Goods_num'),formatter:function(value,row){
                             return '<span style="color:red">'+value+'</span>';
                        }, operate: false},
                        {field: 'unit', title: __('Goods.unit'),operate: false},
                        // {field: 'place_origin', title: __('Goods.place_origin')},
                        // {field: 'company', title: __('Company')},
                        {field: 'operator', title: __('Operator')},
                        {field: 'name', title: __('Name'),searchList: $.getJSON(Config.this_url+"/config/zaxconfig?states=1")},
                        {field: 'needtime', title: __('Needtime'), formatter: Table.api.formatter.datetime, datetimeFormat:"YYYY-MM-DD"},
                        // {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'status', title: __('status'),searchList: $.getJSON(Config.this_url+"/config/zaxconfig?states=15")},
                        {field: 'operate', title: __('Operate'), table: table,buttons: [
                                {
                                    name: 'examine',
                                    title: __('隐藏'),
                                    classname: 'btn btn-xs btn-success btn-click editstatus',
                                    icon: 'fa fa-eye',
                                    hidden:function(row){

                                    },
                                    click: function(data){
                                        $(this).parents('tr').remove()
                                    }},

                            ], events: Table.api.events.operate, formatter: function (value, row, index) {
                                var that = $.extend({}, this);
                                var table = $(that.table).clone(true)
                                // $(table).data("operate-del", null); // 列表页面隐藏 .编辑operate-edit - 删除按钮operate-del
                                // console.log(row)
                                if(row['ystatus'] != 1){

                                    $(table).data("operate-edit", null);// 列表页面隐藏 .编辑operate-edit - 删除按钮operate-del
                                    $(table).data("operate-del", null);// 列表页面隐藏 .编辑operate-edit - 删除按钮operate-del
                                }
                                that.table = table;
                                return Table.api.formatter.operate.call(that, value, row, index);
                            }}
                    ]
                ]
            });
            $('body').on('mouseenter','.title',function () {
                if (this.offsetWidth < this.scrollWidth) {
                    var that = this;
                    var text = $(this).text();
                    layer.tips(text, that,{
                        tips: 1,
                        time: 2000         //设置显示时间
                    });
                }
            })
            $('body').on('mouseenter','.specs',function () {
                if (this.offsetWidth < this.scrollWidth) {
                    var that = this;
                    var text = $(this).text();
                    layer.tips(text, that,{
                        tips: 1,
                        time: 2000         //设置显示时间
                    });
                }
            })
            // 为表格绑定事件
            Table.api.bindevent(table);

            //生成采购单
            $('.btn-addorder').click(function () {
                layer.msg('是否生成采购单', {
                    time: 20000, //20s后自动关闭
                    btn: ['确定', '取消'],
                    btn1:function () {
                        var ids = new Array();
                        $('tbody').find('tr').each(function () {
                            if ($(this).find('input').eq(0).is(':checked')){
                                ids.push($(this).find('.gid').html())
                            }
                        })

                        if(ids.length <1){
                            layer.msg('未选择商品')
                        }else{
                            loadingFlag= layer.msg('正在效验数据', { icon: 16, shade: 0.01,shadeClose:false,time:60000 });
                            $.post(Config.this_url+'/purchase/goods/check',{ids:JSON.stringify(ids)},function (res) {
                                layer.close(loadingFlag);

                                if(res.code == 1){
                                    layer.open({
                                        type: 2, // 1-在content中写html代码， 2-在content中写src路径
                                        area: ['100%', '100%'],
                                        title: "生成采购单",
                                        content: Config.this_url+'/purchase/order/add?ids='+res.data.ids+'&supplier_id='+res.data.supplier_id+'&storehouse_id='+res.data.storehouse_id,
                                        // content: Config.this_url+'/purchase/requisition',
                                    })
                                    // loadingFlag= layer.msg(res.msg, { icon: 16, shade: 0.01,shadeClose:false,time:60000 });
                                }else{
                                    layer.msg(res.msg,{icon: 5})
                                    // loadingFlag= layer.msg(res.msg, { icon: 16, shade: 0.01,shadeClose:false,time:60000 });
                                }
                            })
                        }
                    }
                });
            })


        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});